package com.rm.wechatapp.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rm.wechatapp.entity.AccountRelaFavorite;
import com.rm.wechatapp.entity.AccountRelaWrong;
import com.rm.wechatapp.entity.AccountRelaWrong;
import com.rm.wechatapp.entity.OnQuestion;
import com.rm.wechatapp.entity.VO.QuestionVO;
import com.rm.wechatapp.mapper.AccountRelaWrongMapper;
import com.rm.wechatapp.service.IAccountRelaWrongService;
import com.rm.wechatapp.service.IAccountUserService;
import com.rm.wechatapp.service.IOnQuestionService;
import com.rm.wechatapp.service.IWeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/wrong")
public class AccountRelaWrongController {

    @Autowired
    private IAccountRelaWrongService accountRelaWrongService;

    @Autowired
    private AccountRelaWrongMapper accountRelaWrongMapper;

    @Autowired
    private IWeixinService weixinService;

    @Autowired
    private IAccountUserService accountUserService;

    @Autowired
    private IOnQuestionService coreQuestionService;

    @GetMapping("/list/{code}")
    @ResponseBody
    public List<QuestionVO> list(@PathVariable String code) {
        String openId = weixinService.getOpenId(code);
        Long userId = accountUserService.getByOpenId(openId).getId();
        System.out.println("错题: 用户ID为: " + userId);
        List<AccountRelaWrong> list = accountRelaWrongService.selectByUserId(userId);
        List<OnQuestion> pre_list = new ArrayList<>();
        for (AccountRelaWrong ele : list) {
            System.out.println("错题: 关联信息为: " + ele);
            OnQuestion onQuestion = coreQuestionService.selectById(ele.getQuestionId());
            System.out.println("错题: 题目:" + onQuestion);
            System.out.println("错题: 测试题目" + coreQuestionService.selectById(13L));
            pre_list.add(onQuestion);
        }
        int i = 1;
        List<QuestionVO> post_list = new ArrayList<>();
        for(OnQuestion onQuestion : pre_list) {
            QuestionVO vo = new QuestionVO();
            vo.setId(i);
            vo.setOwner_id(onQuestion.getOwnerId().toString());
            vo.setTitle(onQuestion.getTitle());
            vo.setOptions(JSONUtil.parseArray(onQuestion.getOptions()).toList(String.class));
            vo.setAnswer(JSONUtil.parseArray(onQuestion.getAnswer()).toList(String.class));
            vo.setExplain_skill(onQuestion.getExplainSkill());
            vo.setQuestion_explain(onQuestion.getQuestionExplain());
            vo.setImage(onQuestion.getImage());
            vo.setReal_id(onQuestion.getId());
            i++;
            System.out.println("向结果中添加错题:"+vo);
            post_list.add(vo);
        }
        return post_list;
    }

    @GetMapping("/add")
    @ResponseBody
    public boolean add(
            @RequestParam("code") String code,
            @RequestParam("questionId") Long questionId) {
        AccountRelaWrong accountRelaWrong = new AccountRelaWrong();
        String openId = weixinService.getOpenId(code);
        Long userId = accountUserService.getByOpenId(openId).getId();
        accountRelaWrong.setUserId(userId);
        accountRelaWrong.setQuestionId(questionId);
        System.out.println("测试"+accountRelaWrong);
        QueryWrapper<AccountRelaWrong> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId)
                .eq("user_id", userId);
        if(accountRelaWrongService.count(wrapper) > 0) {
            return false;
        }
        accountRelaWrongService.save(accountRelaWrong);
        return true;
    }

    @GetMapping("/delete")
    @ResponseBody
    public boolean delete(@RequestParam("code") String code,
                          @RequestParam("questionId") Long questionId) {
        QueryWrapper<AccountRelaWrong> queryWrapper = new QueryWrapper<>();
        String openId = weixinService.getOpenId(code);
        Long userId = accountUserService.getByOpenId(openId).getId();
        queryWrapper.eq("user_id", userId)
                .eq("question_id", questionId);
        AccountRelaWrong accountRelaWrong = accountRelaWrongMapper.selectOne(queryWrapper);
        accountRelaWrongMapper.deleteById(accountRelaWrong.getId());
        return true;
    }

    @GetMapping("/deletes")
    @ResponseBody
    public int deletes(){
        String openId = weixinService.getOpenId(null);
        return 789;
    }

    @GetMapping("warning/delete/all")
    @ResponseBody
    public boolean deleteAll() {
        accountRelaWrongMapper.delete(new QueryWrapper<AccountRelaWrong>().ge("id",0));
        return true;
    }

}
